/* USER CODE BEGIN Header */
/* you can remove the copyright */
/*
 *  Copyright 2020-2024 Yuntu Microelectronics co.,ltd
 *  All rights reserved.
 * 
 *  YUNTU Confidential. This software is owned or controlled by YUNTU and may only be
 *  used strictly in accordance with the applicable license terms. By expressly
 *  accepting such terms or by downloading, installing, activating and/or otherwise
 *  using the software, you are agreeing that you have read, and that you agree to
 *  comply with and are bound by, such license terms. If you do not agree to be
 *  bound by the applicable license terms, then you may not retain, install,
 *  activate or otherwise use the software. The production use license in
 *  Section 2.3 is expressly granted for this software.
 * 
 * @file main.c
 * @brief 
 * 
 */

/* USER CODE END Header */
#include "sdk_project_config.h"
/* Includes ------------------------------------------------------------------*/

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */   
#include "sys_task.h"
#include "app_etmr.h"
#include "app_exv.h" 
#include "app_motors.h"
#include "app_uart_drv.h"

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define TMR_INST 0

#define CAPTURE_INST_1 (2)
#define CAPTURE_CHAN_1 (1)

#define CAPTURE_INST_2 (2)
#define CAPTURE_CHAN_2 (0)

extern volatile uint16_t High_Duty_1,Period_1,Duty_1,Duty_Now_1;
extern volatile uint16_t High_Duty_2,Period_2,Duty_2,Duty_Now_2;

extern volatile uint8_t Flag_4Way;
extern volatile uint8_t Flag_Mode;

extern volatile int motor_angle;
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */

/* Private function declare --------------------------------------------------*/
/* USER CODE BEGIN PFDC */
/* USER CODE END PFDC */
static void Board_Init(void);

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */


/**
 * @brief  The application entry point.
 * @retval int
 */
int main(void)
{
    /* USER CODE BEGIN 1 */     
    // 选择ADC触发源由TMU触发
    CIM->CTRL |= CIM_CTRL_ADC0_TRIG_SEL(1);
    /* USER CODE END 1 */ 
    Board_Init();
    /* USER CODE BEGIN 2 */
    
    /****** ETMR *******/
	eTMR0->SYNC |= eTMR_SYNC_GLOB_MASK;         //生成全局时间基准信号
	eTMR1->SYNC |= eTMR_SYNC_GLOB_MASK;         //生成全局时间基准信号
     
    /* USER CODE END 2 */

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    sys_TaskInit();            
    INT_SYS_EnableIRQGlobal();
    while (1) 
	{
      /* USER CODE END WHILE */
      /* USER CODE BEGIN 3 */
        
        sys_LoopTask();
			/* USER CODE END WHILE */
			/* USER CODE BEGIN 3 */
    }
    /* USER CODE END 3 */
}

static void Board_Init(void)
{
    CLOCK_SYS_Init(g_clockManConfigsArr,CLOCK_MANAGER_CONFIG_CNT,g_clockManCallbacksArr,CLOCK_MANAGER_CALLBACK_CNT);
    CLOCK_SYS_UpdateConfiguration(CLOCK_MANAGER_ACTIVE_INDEX,CLOCK_MANAGER_POLICY_AGREEMENT);
    PINS_DRV_Init(NUM_OF_CONFIGURED_PINS0,g_pin_mux_InitConfigArr0);
    TMU_DRV_Init(0,&tmu_config0);
    UTILITY_PRINT_Init();
    ADC_DRV_ConfigConverter(0,&adc_config0);
    eTMR_DRV_Init(0,&ETMR_CM_Config0,&ETMR_CM_Config0_State);
    eTMR_DRV_Init(1,&ETMR_CM_Config1,&ETMR_CM_Config1_State);
    eTMR_DRV_InitPwm(0,&ETMR_PWM_Config0);
    eTMR_DRV_InitPwm(1,&ETMR_PWM_Config1);
    eTMR_DRV_Enable(0);
    eTMR_DRV_Enable(1);
    eTMR_DRV_Init(2,&ETMR_CM_Config2,&ETMR_CM_Config2_State);
    eTMR_DRV_InitInputCapture(2,&ETMR_IC_Config2);
    eTMR_DRV_Enable(2);
    eTMR_DRV_EnableInterrupts(2,0x1);
}

/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
